package com.my.ecommercesystem.controller;


import com.my.ecommercesystem.dto.MyAddress;
import com.my.ecommercesystem.dto.MyMessage;
import com.my.ecommercesystem.entity.Goods;
import com.my.ecommercesystem.entity.User;
import com.my.ecommercesystem.entity.UserAddress;
import com.my.ecommercesystem.service.UserService;
import com.my.ecommercesystem.utils.IsFish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/mytest")

    public String mytest(HttpServletRequest request) {

        List<User> list = userService.getAllUser();

        request.setAttribute("list", list);

        return "index";

    }
    @RequestMapping("/mytest1")
    public ModelAndView mytest1(HttpServletRequest request,Integer id) {

        ModelAndView modelAndView=new ModelAndView();
        Goods goods=new Goods();
        goods.setGoods_name("水果");
       modelAndView.setViewName("main");
       modelAndView.addObject("goods",id);
        return modelAndView;

    }

    @RequestMapping(value = "/register1")
    @ResponseBody
    public void register1(User user,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(userService.queryUserByPhone(user.getUser_phone())==null){
            boolean flag = userService.insertUser(user);
            if(flag){
                //重新查询user
                //User loginuser = us.userLogin(user.getUser_name(), user.getUser_pwd());
                out.print("<script>alert('注册成功！');location.href='login';</script>");
                out.flush();
                out.close();

            }
        }else if(user.getUser_name().equals("")||user.getUser_phone().equals("")||user.getUser_pwd().equals("")) {
            //登入失败 跳转到注册界面
            out.print("<script>alert('注册失败');location.href='register'</script>");
            out.flush();
            out.close();
        }

    }

    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request) {

        ModelAndView modelAndView=new ModelAndView();


        modelAndView.setViewName("main");

        return modelAndView;

    }

    @RequestMapping("/user_center_info")
    public String user_center_info(HttpSession session){
        User user = userService.getUserById(1);
        session.setAttribute("loginuser",user);
        return "user_center_info";
    }

    @RequestMapping("/user_center_order")
    public String user_center_order(){

        return "user_center_order";
    }

    @RequestMapping("/user_center_site")
    public String user_center_site(HttpSession session, HttpServletResponse response) throws Exception{
        User user = (User) session.getAttribute("loginuser");
        if(user!=null && user.getUser_id()!=null){
            List<UserAddress> AddressList = userService.getAddressByUserId(user.getUser_id());
        }else{
            PrintWriter out = response.getWriter();
            out.print("<script>alert('尚未登录，请先登录')</script>");
            return "login";
        }
        return "user_center_site";
    }

    @RequestMapping("/GetUserAddress")
    @ResponseBody
    public MyAddress GetUserAddress(Integer user_id){
        MyAddress message = new MyAddress();
        if(user_id!=null){
            //查询用户所有通讯地址
            List<UserAddress> AddressList = userService.getAddressByUserId(user_id);
            if(AddressList.size()>0){
                message.setFlag(1);
                message.setAddressList(AddressList);
            }else{
                message.setFlag(1);
                message.setInfo("尚未添加地址");
            }
//            //查询用户设置的默认地址
//            List<UserAddress>defaultAddress = userService.getDefaultAddressByUserId(user_id);
//            if(defaultAddress.size()>1){
//                message.setFlag(1);
//                message.setDefaultAddress(defaultAddress.get(0));
//            }else{
//                message.setFlag(1);
//                message.setInfo("尚未添加地址");
//            }
        }else{
            message.setFlag(0);
            message.setInfo("尚未登录");
        }
        return message;
    }

    @RequestMapping("/deleteAddress")
    public String deleteAddress(HttpServletResponse response,Integer address_id)throws Exception {
        PrintWriter out = response.getWriter();
        if (userService.removeAddressById(address_id)) {
            out.print("<script>alert('删除成功！');location.href='user_center_site';</script>");
        } else {
            out.print("<script>alert('删除失败，请重新操作！';history.go(-1));</script>");
        }
        out.flush();
        out.close();
        return "user_center_site";
    }

    @RequestMapping("/addAddress")
    public void deleteAddress(HttpServletResponse response,HttpSession session,UserAddress userAddress)throws Exception {
        PrintWriter out = response.getWriter();
        User user= (User) session.getAttribute("loginuser");
        userAddress.setAddress_userid(user.getUser_id());
        if(userAddress.getAddress_name()==null||userAddress.getAddress_name().equals("")||
           userAddress.getAddress_phone()==null||userAddress.getAddress().equals("")||
           userAddress.getAddress()==null||userAddress.getAddress().equals("")){
            out.print("<script>alert('填写信息不完整！');history.go(-1)';</script>");
        }else{
            if(userService.addUserAddress(userAddress)){
                out.print("<script>alert('添加成功！');location.href='user_center_site';</script>");
            }else{
                out.print("<script>alert('添加失败，请重新操作');history.go(-1)';</script>");
            }
        }
        out.flush();
        out.close();
    }

    @RequestMapping("/checkLogin")
    @ResponseBody
    public  void checkLogin(HttpSession session,HttpServletRequest request,HttpServletResponse response,String user_phone,String user_pwd)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        IsFish isFish=new IsFish();
        if (isFish.isFish(request)){
            User user  =userService.login(user_pwd,user_phone);
            if (user!=null){

                out.print("<script>alert('登录成功');location.href='main'</script>");
                session.setAttribute("loginuser",user);

            }else{
                out.print("<script>alert('登录失败');history.go(-1);</script>");
            }
        }else{
            out.print("<script>alert('滚吧，盗链狗！！！！');history.go(-1);</script>");
        }

        out.flush();
        out.close();




    }

    @RequestMapping("/editAddress")
    public void eidtAddress(HttpServletResponse response,UserAddress userAddress)throws Exception {
        PrintWriter out = response.getWriter();
        if(userAddress.getAddress_id()==null || userAddress.getAddress_id()<0){
            if(userAddress.getAddress_name()==null||userAddress.getAddress_name().equals("")||
                    userAddress.getAddress_phone()==null||userAddress.getAddress().equals("")||
                    userAddress.getAddress()==null||userAddress.getAddress().equals("")){
                out.print("<script>alert('所有字段不能为空！');history.go(-1)';</script>");
            }else{
                if(userService.editAddressById(userAddress)){
                    out.print("<script>alert('修改成功！');location.href='user_center_site';</script>");
                }else{
                    out.print("<script>alert('修改失败，请重新操作');history.go(-1)';</script>");
                }
            }
        }else{
            out.print("<script>alert('参数有误，请重新操作！');location.href='user_center_site';</script>");
        }
        out.flush();
        out.close();
    }

}
